$(function () {
    $("#jqGrid").jqGrid({
        url: '../jobchangemanagement/list',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
            { label: '姓名', name: 'name', index: 'name', width: 80 },
            { label: '身份证号', name: 'idno', index: 'idno', width: 80 ,hidden:true},
            // { label: '入职日期', name: 'joinDate', index: 'join_date',  width: 90, formatter:"date", formatoptions: {newformat:'Y-m-d'}  },
            { label: '公司', name: 'company', index: 'company', width: 80 },
            { label: '公司id', name: 'companyId', index: 'company_id', width: 80,hidden:true },
            { label: '原部门', name: 'originalDepartment', index: 'Original_department', width: 80 },
            { label: '原岗位', name: 'originalPost', index: 'Original_post', width: 80,formatter:getDictInfo },
            { label: '原岗位id', name: 'originalPostId', index: 'Original_post_id', width: 80 ,hidden:true},
            // { label: '原岗位级别', name: 'originalPostLevel', index: 'Original_post_level', width: 80 },
            { label: '原基本工资', name: 'originalBaseSalary', index: 'Original_base_salary', width: 80 },
            { label: '原绩效工资', name: 'originalAchievementsSalary', index: 'Original_Achievements_salary', width: 80 },
            { label: '原加班费', name: 'originalOvertimePay', index: 'Original_overtime_pay', width: 80 },
            { label: '异动类型', name: 'changeType', index: 'change_type', width: 80,formatter:getDictInfo },

            { label: '现公司', name: 'presentUnit', index: 'Present_unit', width: 80 },
            { label: '现公司id', name: 'presentUnitId', index: 'Present_unit_id', width: 80,hidden:true },
            { label: '现部门', name: 'presentDepartment', index: 'Present_department', width: 80 },
            { label: '现岗位', name: 'presentPost', index: 'Present_post', width: 80 ,formatter:getDictInfo},
            { label: '现岗位id', name: 'presentPostId', index: 'Present_post_id', width: 80 ,hidden:true},
            // { label: '现岗位级别', name: 'presentPostLevel', index: 'Present_post_level', width: 80 },
            { label: '现基本工资', name: 'presentBaseSalary', index: 'Present_base_salary', width: 80 },
            { label: '现绩效工资', name: 'presentAchievementsSalary', index: 'Present_Achievements_salary', width: 80 },
            { label: '现加班费', name: 'presentOvertimePay', index: 'Present_overtime_pay', width: 80 },
            { label: '其他说明', name: 'remark', index: 'remark', width: 80 },
            { label: '录入人', name: 'cusername', index: 'cusername', width: 80 },
            { label: '录入日期', name: 'cdate', index: 'cdate', width: 80, width: 90, formatter:"date", formatoptions: {newformat:'Y-m-d'}  },
            { label: '一级审核人', name: 'firApprover', index: 'fir_approver', width: 80 },
            { label: '一级审核日期', name: 'firApprovalDate', index: 'fir_approval_date',  width: 90, formatter:"date", formatoptions: {newformat:'Y-m-d'} },
            { label: '二审核人', name: 'secApprover', index: 'sec_approver', width: 80 },
            { label: '二级审核日期', name: 'secApprovalDate', index: 'sec_approval_date',  width: 90, formatter:"date", formatoptions: {newformat:'Y-m-d'}  },
            { label: '三级审核人', name: 'thiApprover', index: 'thi_approver', width: 80 },
            { label: '三级审核日期', name: 'thiApprovalDate', index: 'thi_approval_date',  width: 90, formatter:"date", formatoptions: {newformat:'Y-m-d'}  }
        ],

		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        shrinkToFit:false,
        autoScroll: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    vm.getDictList();
    utils.popGrid();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            year: null
        },
		showList: true,
		title: null,
		jobChangeManagement: {},
        dictList:{},
        dictList1:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.jobChangeManagement = {
                name:"",
                presentDepartment:""
            };
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}

            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            if(rowData.firApprover!=null&&rowData.firApprover!="")
            {
                alert("已进行过审核，不能修改");
                return ;
            }
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.jobChangeManagement.id == null ? "../jobchangemanagement/save" : "../jobchangemanagement/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.jobChangeManagement),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			for(var i=0;i<ids.length;i++){
            var rowData=$("#jqGrid").jqGrid("getRowData",ids[i]);
            if(rowData.firApprover!=null&&rowData.firApprover!="")
            {
                alert("已审核，不能修改");
                return ;
             }
            }
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../jobchangemanagement/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../jobchangemanagement/info/"+id, function(r){
                vm.jobChangeManagement = r.jobChangeManagement;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'presentUnit': vm.q.presentUnit,
                    'company': vm.q.company,'name': vm.q.name,
                    'presentDepartment': vm.q.presentDepartment},
                page:page
            }).trigger("reloadGrid");
		},
        firApprove: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            if(rowData.firApprover!=null&&rowData.firApprover!="")
            {
                alert("一级审核已审核过，请不要再次审核！");
                return ;
            }
            var params={"id":id,
                "type":"fir"};

                $.ajax({
                    type: "POST",
                    url: "../jobchangemanagement/approve",
                    contentType: "application/json",
                    data: JSON.stringify(params),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
        },
        secApprove: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            if(rowData.firApprover==null||rowData.firApprover=="")
            {
                alert("请先进行一级审核！");
                return ;
            }
            var params={"id":id,
                "type":"sec"};

            $.ajax({
                type: "POST",
                url: "../jobchangemanagement/approve",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                            $("#jqGrid").trigger("reloadGrid");
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        thiApprove: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);

            if(rowData.secApprover==null||rowData.secApprover=="")
            {
                alert("请先进行二级审核！");
                return ;
            }
            var params={"id":id,
                "type":"thi"};

            $.ajax({
                type: "POST",
                url: "../jobchangemanagement/approve",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                            $("#jqGrid").trigger("reloadGrid");
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },

 /*       popUser: function(dtlindex){
            popPage.popUserGrid();
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择员工",
                area: ['750px', '400px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#popUser"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var grid = $("#popUserGrid");
                    var rowKey = grid.getGridParam("selrow");
                    if(!rowKey){
                        alert("请选择一条记录");
                        return ;
                    }
                    var rowData=grid.jqGrid("getRowData",rowKey);
                    vm.jobChangeManagement.name=rowData.userdesc;
                    vm.jobChangeManagement.userId=rowData.userId;
                    vm.jobChangeManagement.idno=rowData.idno;
                    vm.jobChangeManagement.company=rowData.company;
                    vm.jobChangeManagement.companyid=rowData.companyId;
                    vm.jobChangeManagement.originalDepartment=rowData.dept;
                    vm.jobChangeManagement.originalDepartmentId=rowData.deptNo;

                    // vm.jobChangeManagement.joinDate=rowData.joinDate;
                    vm.jobChangeManagement.originalPost=rowData.post;
                    // vm.jobChangeManagement.originalPostLevel=rowData.postLevel;
                    layer.close(index);
                }
            });
        },*/


        popUser: function(){
           // utils.popGrid();
            var page = $("#popGrid").jqGrid('getGridParam','page');
            $("#popGrid").jqGrid('setGridParam',{
                postData:{
                    'iswork': '1',
                    'isHuashun': 'Y',
                },
                page:page
            }).trigger("reloadGrid");
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择员工",
                area: ['800px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#popLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var grid = $("#popGrid");
                    var rowKey = grid.getGridParam("selrow");
                    if(!rowKey){
                        alert("请选择一条记录");
                        return ;
                    }
                    var rowData=grid.jqGrid("getRowData",rowKey);
                    vm.jobChangeManagement.name=rowData.name;
                    vm.jobChangeManagement.idno=rowData.idno;
                    vm.jobChangeManagement.company=rowData.company;
                    vm.jobChangeManagement.companyid=rowData.companyId;
                     vm.jobChangeManagement.originalDepartment=rowData.department;
                    vm.jobChangeManagement.joinDate=rowData.joinDate;
                    vm.jobChangeManagement.originalPost=rowData.post;
                    vm.jobChangeManagement.originalPostLevel=rowData.postLevel;

                    //alert(JSON.stringify( vm.safetyEducationManagement));


                    layer.close(index);
                }
            });
        },
        getDictList: function(){
            var params = {
                dicttype:"ydlx,station"
            }
            $.ajax({
                type: "POST",
                url: "../dictutils/getDictList",
                contentType: "application/json",
                data: JSON.stringify(params),
                dataType:"json",
                async:false,
                cache:false,
                success: function(r){
                    if(r.code === 0){
                        vm.dictList = r.ydlx;
                        vm.dictList1 = r.station;
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },

        selectDept: function(){
                $("#popOfficeGrid").jqGrid({
                    url: '../empInfo/listOffice',
                    postData:{
                        'isHuashun': 'Y',
                    },
                    datatype: "json",
                    colModel: [
                        { label: '公司ID', name: 'companyId', index: "company_id", width: 45, key: true,hidden:true },
                        { label: '公司', name: 'company', index: "company", width: 120 },
                        { label: '部门', name: 'department', index: "department", width: 80 },
                        { label: '部门id', name: 'departmentId', index: "department_id", width: 80 ,hidden:true },
                        { label: '岗位', name: 'post', width: 80 },
                    ],
                    viewrecords: true,
                    height: 400,
                   /* rowNum: 100,
                     rowList : [10,30,50],*/
                     rownumbers: true,
                     rownumWidth: 25,
                    autowidth:true,
                    multiselect: false,
                    shrinkToFit:false,
                    autoScroll: false,
                    // pager: "#popOfficeGridPager",
                    jsonReader : {
                        root: "page.list",
                        page: "page.currPage",
                        total: "page.totalPage",
                        records: "page.totalCount"
                    },
                    prmNames : {
                        page:"page",
                        rows:"limit",
                        order: "order"
                    },
                    gridComplete:function(){
                        //隐藏grid底部滚动条
                        $("#popOfficeGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                    }
                });


            // popPage.popOfficeGrid();
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择部门",
                area: ['370px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#popOffice"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var grid = $("#popOfficeGrid");
                    var rowKey = grid.getGridParam("selrow");
                    if(!rowKey){
                        alert("请选择一条记录");
                        return ;
                    }

                    var rowData=grid.jqGrid("getRowData",rowKey);
                    /*  if(rowData.isConmpany=='Y'){
                     alert("不能选择公司，请选择部门");
                     return ;
                     }*/
                    vm.jobChangeManagement.presentDepartment = rowData.department;
                    vm.jobChangeManagement.presentDepartmentId = rowData.departmentId;
                    vm.jobChangeManagement.presentUnit = rowData.company;
                    vm.jobChangeManagement.presentUnitId=rowData.id;
                    vm.jobChangeManagement.presentPost=rowData.post;



                    layer.close(index);
                }
            });
        },
	}
});